

#include <iostream>
#include <vector>
#include <stack>
#include <unordered_set>
#include <set>

using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(nullptr) {
	}
};
class Solution {
public:
	size_t GetOneListLength(ListNode* pHead) {
		size_t len = 0;
		while(pHead){
			++len;
			pHead = pHead->next;
		}
		return len;
	}
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
		if (pHead1 == nullptr || pHead2 == nullptr) return nullptr;

		ListNode* common_node = nullptr;
		size_t p1_size = GetOneListLength(pHead1);
		size_t p2_size = GetOneListLength(pHead2);
		if (p1_size > p2_size) {
			size_t pos_diss = p1_size - p2_size;
			while(pos_diss) {
				pHead1 = pHead1->next;
				--pos_diss;
			}
		}else if (p2_size > p1_size) {
			size_t pos_diss = p2_size - p1_size;
			while(pos_diss) {
				pHead2 = pHead2->next;
				--pos_diss;
			}
		}
		while (pHead1 && pHead2) {
			if (pHead1 == pHead2){
				common_node = pHead1;
				break;
			}
			pHead1 = pHead1 ->next;
			pHead2 = pHead2 ->next;
		}
		return common_node;
        
    }
};